package AS_source
{
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class ProfSearch
	{
		import Components.*;
		import mx.containers.Accordion;
		import mx.containers.ApplicationControlBar;
		import mx.containers.TabNavigator;
		import mx.containers.VBox;
		import mx.containers.HBox;
		import mx.containers.Panel;
		import mx.controls.Alert;
		import mx.controls.ComboBox;
		import mx.controls.Text;
		import mx.controls.TextArea;
		import mx.rpc.events.FaultEvent;
		import flash.events.Event;
		public var genFieldListing:Array=[{label: "Adelphi University", data: "adelphi_university"}, {label: "Arizona State University", data: "arizona_state_university"}, {label: "Auburn University", data: "auburn_university"}, {label: "Bard College", data: "bard_college"}, {label: "Barnard College", data: "barnard_college"}, {label: "Baruch College", data: "baruch_college"}, {label: "Baylor University", data: "baylor_university"}, {label: "Boston College", data: "boston_college"}, {label: "Boston University", data: "boston_university"}, {label: "Brigham Young University", data: "brigham_young_university"}, {label: "Brooklyn College", data: "brooklyn_college"}, {label: "Brown University", data: "brown_university"}, {label: "California Institute of Technology", data: "california_institute_of_technology"}, {label: "Carnegie Mellon University", data: "carnegie_mellon_university"}, {label: "City College of New York", data: "city_college_of_new_york"},{label: "Clemson University", data: "clemson_university"}, {label: "Columbia University", data: "columbia_university"}, {label: "Cooper Union", data: "cooper_union_for_the_advancement_of_science_and_art"}, {label: "Cornell University", data: "cornell_university"}, {label: "Dartmouth College", data: "dartmouth_college"}, {label: "Depaul University", data: "depaul_university"}, {label: "Duke University", data: "duke_university"}, {label: "Emory University", data: "emory_university"}, {label: "Fairfield University", data: "fairfield_university"}, {label: "Fairleigh Dickinson University", data: "fairleigh_dickinson_university"}, {label: "Fashion Institute of Technology", data: "fashion_institute_of_technology"}, {label: "Florida State University", data: "florida_state_university"}, {label: "Fordham University", data: "fordham_university"}, {label: "Fresno State University", data: "fresno_state_university"}, {label: "Georgetown University", data: "georgetown_university"}, {label: "Georgia Institute of Technology", data: "georgia_institute_of_technology"},  {label: "Harvard University", data: "harvard_university"}, {label: "Iowa State University", data: "iowa_state_university"}, {label: "Johns Hopkins University", data: "johns_hopkins_university"}, {label: "Juilliard School", data: "juilliard_school"},  {label: "Lehman College", data: "lehman_college"}, {label: "Long Island University", data: "long_island_university"}, {label: "Louisiana Technical University", data: "louisiana_tech_university"}, {label: "Manhattan College", data: "manhattan_college"}, {label: "Manhattan School of Music", data: "manhattan_school_of_music"}, {label: "Manhattanville College", data: "manhattanville_college"}, {label: "Marist College", data: "marist_college"}, {label: "Marquette University", data: "marquette_university"}, {label: "Marshall University", data: "marshall_university"}, {label: "Massachusetts Institute of Technology", data: "massachusetts_institute_of_technology"}, {label: "McGill University", data: "mcgill_university"}, {label: "Michigan State University", data: "michigan_state_university"}, {label: "Mississippi State University", data: "mississippi_state_university"}, {label: "Montclair State University", data: "montclair_state_university"}, {label: "Mount Holyoke College", data: "mount_holyoke_college"}, {label: "New Jersey Institute of Technology", data: "new_jersey_institute_of_technology"}, {label: "New School University", data: "new_school_university"}, {label: "New York University", data: "new_york_university"}, {label: "North Carolina State University", data: "north_carolina_state_university"}, {label: "Northeastern University", data: "northeastern_university"}, {label: "Northwestern University", data: "northwestern_university"}, {label: "Ohio State University", data: "ohio_state_university"}, {label: "Oklahoma State University", data: "oklahoma_state_university"}, {label: "Old Dominion University", data: "old_dominion_university"}, {label: "Oregon State University", data: "oregon_state_university"}, {label: "Pace University", data: "pace_university"}, {label: "Pennsylvania State University", data: "pennsylvania_state_university"}, {label: "Pepperdine University", data: "pepperdine_university"}, {label: "Princeton University", data: "princeton_university"}, {label: "Purdue University", data: "purdue_university"}, {label: "Quinnipiac University", data: "quinnipiac_university"}, {label: "Rice University", data: "rice_university"}, {label: "Rockefeller University", data: "rockefeller_university"}, {label: "Rutgers, The State University", data: "rutgers_the_state_university"}, {label: "Sarah Lawrence College", data: "sarah_lawrence_college"}, {label: "Seton Hall University", data: "seton_hall_university"}, {label: "Smith College", data: "smith_college"}, {label: "Southampton College", data: "southampton_college"}, {label: "St John’s University", data: "st_johns_university"}, {label: "Stanford University", data: "stanford_university"}, {label: "State University of New York", data: "state_university_of_new_york"}, {label: "State University of New York at Albany", data: "state_university_of_new_york_at_albany"}, {label: "State University of New York at Purchase", data: "state_university_of_new_york_at_purchase"}, {label: "State University of New York at Stony Brook", data: "state_university_of_new_york_at_stony_brook"}, {label: "Syracuse University", data: "syracuse_university"}, {label: "Temple University", data: "temple_university"}, {label: "Texas A and M University", data: "texas_a_and_m_university"}, {label: "Texas Christian University", data: "texas_christian_university"}, {label: "Texas Tech University", data: "texas_tech_university"}, {label: "United States Air Force Academy", data: "united_states_air_force_academy"}, {label: "United States Naval Academy", data: "united_states_naval_academy"}, {label: "United States Military Academy", data: "united_states_military_academy"}, {label: "University of Alabama", data: "university_of_alabama"}, {label: "University of Arizona", data: "university_of_arizona"}, {label: "University of Arkansas", data: "university_of_arkansas"}, {label: "University of California", data: "university_of_california"}, {label: "University of Chicago", data: "university_of_chicago"}, {label: "University of Cincinnati", data: "university_of_cincinnati"}, {label: "University of Colorado", data: "university_of_colorado"}, {label: "University of Connecticut", data: "university_of_connecticut"}, {label: "University of Delaware", data: "university_of_delaware"}, {label: "University of Florida", data: "university_of_florida"}, {label: "University of Georgia", data: "university_of_georgia"}, {label: "University of Hawaii", data: "university_of_hawaii"}, {label: "University of Houston", data: "university_of_houston"}, {label: "University of Iowa", data: "university_of_iowa"}, {label: "University of Kansas", data: "university_of_kansas"}, {label: "University of Kentucky", data: "university_of_kentucky"}, {label: "University of Louisville", data: "university_of_louisville"}, {label: "University of Maine", data: "university_of_maine"}, {label: "University of Massachusetts", data: "university_of_massachusetts"}, {label: "University of Memphis", data: "university_of_memphis"}, {label: "University of Miami", data: "university_of_miami"}, {label: "University of Michigan", data: "university_of_michigan"}, {label: "University of Minnesota", data: "university_of_minnesota"}, {label: "University of Mississippi", data: "university_of_mississippi"}, {label: "University of Missouri", data: "university_of_missouri"}, {label: "University of Nebraska", data: "university_of_nebraska"}, {label: "University of North Carolina", data: "university_of_north_carolina"}, {label: "University of Notre Dame", data: "university_of_notre_dame"}, {label: "University of Oklahoma", data: "university_of_oklahoma"}, {label: "University of Oregon", data: "university_of_oregon"}, {label: "University of Pennsylvania", data: "university_of_pennsylvania"}, {label: "University of Pittsburgh", data: "university_of_pittsburgh"}, {label: "University of Rhode Island", data: "university_of_rhode_island"}, {label: "University of Richmond", data: "university_of_richmond"}, {label: "University of Rochester", data: "university_of_rochester"}, {label: "University of South Carolina", data: "university_of_south_carolina"}, {label: "University of Southern California", data: "university_of_southern_california"}, {label: "University of Tennessee", data: "university_of_tennessee"}, {label: "University of Texas", data: "university_of_texas"}, {label: "University of Tulsa", data: "university_of_tulsa"}, {label: "University of Utah", data: "university_of_utah"}, {label: "University of Vermont", data: "university_of_vermont"}, {label: "University of Virginia", data: "university_of_virginia"}, {label: "University of Washington", data: "university_of_washington"}, {label: "University of Wisconsin", data: "university_of_wisconsin"}, {label: "University of Wyoming", data: "university_of_wyoming"}, {label: "Vanderbilt University", data: "vanderbilt_university"}, {label: "Vassar College", data: "vassar_college"}, {label: "Villanova University", data: "villanova_university"}, {label: "Virginia Commonwealth University", data: "virginia_commonwealth_university"}, {label: "Virginia Military Institute", data: "virginia_military_institute"}, {label: "Virginia Tech", data: "virginia_polytechnic_institute_and_state_university"}, {label: "Wake Forest University", data: "wake_forest_university"}, {label: "Washington State University", data: "washington_state_university"}, {label: "Washington University", data: "washington_university"}, {label: "Wesleyan University", data: "wesleyan_university"}, {label: "West Virginia University", data: "west_virginia_university"}, {label: "Xavier University of Ohio", data: "xavier_university_of_ohio"}, {label: "Yale University", data: "yale_university"}]

		var tabNavi:TabNavigator;
		var searchNavi:Accordion;
		var searchTabOutput:TextArea;

		public function ProfSearch(tabControl:TabNavigator, searchControl:Accordion)
		{
			tabNavi=tabControl;
			searchNavi=searchControl;
		}

		public function getDataProvider():Array
		{

			return genFieldListing;
		}

		public function searchButtonEvt(e:Event):void
		{
			var profSearch:Profile_Search_VBox=Profile_Search_VBox(searchNavi.getChildByName("profSearch"));
			var controlBar:ApplicationControlBar=ApplicationControlBar(profSearch.getChildByName("profControlBar"));
			var genBox:VBox=VBox(controlBar.getChildByName("profVBox"));
			var selectionData:String=ComboBox(genBox.getChildByName("profComboBox")).selectedItem.data;
			this.setupWriter();
			this.obtainHTML(genURLListing(selectionData));
			tabNavi.selectedIndex = tabNavi.getChildIndex(tabNavi.getChildByName("schoolContents"));
		}

		private function setupWriter()
		{
			var tester:VBox=VBox(tabNavi.getChildByName("schoolContents"));
			var outBox:HBox=HBox(tester.getChildByName("outerBox"));
			var profPanel:Panel=Panel(outBox.getChildByName("profilePanel"));
			var output:TextArea=TextArea(profPanel.getChildByName("profileOutput"));
			searchTabOutput=output;


		}


		function genURLListing(field:String):String
		{
			var prefix:String="http://topics.nytimes.com/top/reference/timestopics/organizations/";
			var suffix:String="/index.html";
			return prefix + field.substr(0, 1) + "/" + field + suffix;
			//optionHTML="";for(var i=0;i<options.length;i++){opt=options[i];if(opt.indexOf("|")!=-1){p=opt.split(/\|/g);opt=p[0];url=p[1];}else{url=opt.toLowerCase().replace(/\s+/g,"_");url=url.charAt(0)+"/"+url;}optionHTML=optionHTML+"<option value='"+prefix+url+suffix+"'>"+opt+"</option>";}


			//return "http://grad-schools.usnews.rankingsandreviews.com/grad/" + field + "/search";	
		}

//generate the Specific Listing within the General Field
		function specURLListing(field:String, specField):String
		{
			return "http://grad-schools.usnews.rankingsandreviews.com/grad/" + field + "/" + specField;
		}

		function obtainHTML(url:String):void
		{
			var assetLoader:URLLoader=new URLLoader();
			var request:URLRequest=new URLRequest(url);
			assetLoader.load(request);
			assetLoader.addEventListener(Event.COMPLETE, loadPageComplete);
			assetLoader.addEventListener(FaultEvent.FAULT, failureHandler);
		}

		public function getfieldListing():Array
		{
			return this.genFieldListing;
		}

		function loadPageComplete(e:Event):void
		{

			var pageText:String=String(e.currentTarget.data);
			var oppattern:RegExp=/<p\sclass="summary" /gx;
			var clpattern:RegExp=/<\/p>/gx;
			var closedTag:String=String(clpattern);
			var openIndex:int=pageText.search(oppattern);
			var closeIndex:int=pageText.search(clpattern);
			var tableText:String=pageText.substring(openIndex, closeIndex + 4);
			searchTabOutput.htmlText=tableText;
			searchTabOutput.editable=false;


			//var output:String = outputField.text;
			//Alert.show("Output is:" + output);
			//outputField.text += tableText;
		}

		function failureHandler(e:Event):void
		{
			Alert.show("HTML Loading Error");
		}




	}
}